﻿using System;
using System.Web.UI.WebControls;

namespace ECommerce.Administration.Controls
{
	public partial class ProductPictures : AdminBaseUsercontrol
	{
		private Service.IPictureService _PictureService;
		private Service.IProductPictureService _ProductPictureService;
		private int _ProductID;

		public int ProductID
		{
			get
			{
				if (ViewState["ProductID"] != null)
				{
					_ProductID = (int)ViewState["ProductID"];

					return _ProductID;
				}

				_ProductID = CommonHelper.QueryStringInt("productID");
				return _ProductID;
			}
			set
			{
				ViewState["ProductID"] = value;
				_ProductID = value;
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (ProductID > 0)
			{
				pnlData.Visible = true;
				pnlMessage.Visible = false;

				btnUploadProductPicture.Text = GetLocalResourceString("Admin.ProductPictures.UploadProductPictureButtonText");

				BindData();
			}
			else
			{
				pnlData.Visible = false;
				pnlMessage.Visible = true;
			}
		}

		protected void btnUploadProductPicture_Click(object sender, EventArgs e)
		{
			if (_PictureService == null)
			{
				_PictureService = new Service.PictureService();
			}

			if (_ProductPictureService == null)
			{
				_ProductPictureService = new Service.ProductPictureService();
			}

			if (fuProductPicture1.HasFile)
			{
				Data.Entity.Picture picture = new Data.Entity.Picture();

				picture.Extension = fuProductPicture1.PostedFile.ContentType;
				picture.IsNew = true;
				picture.PictureBinary = fuProductPicture1.FileBytes;

				picture.PictureID = _PictureService.CreateNewPicture(picture);

				if (picture.PictureID > 0)
				{
					Data.Entity.ProductPicture productPicture = new Data.Entity.ProductPicture();

					productPicture.DisplayOrder = Convert.ToInt32(TextDisplayOrder1.Text.Trim());
					productPicture.PictureID = picture.PictureID;
					productPicture.ProductID = ProductID;

					_ProductPictureService.CreateNewProductPicture(productPicture);
				}
			}

			if (fuProductPicture2.HasFile)
			{
				Data.Entity.Picture picture = new Data.Entity.Picture();

				picture.Extension = fuProductPicture2.PostedFile.ContentType;
				picture.IsNew = true;
				picture.PictureBinary = fuProductPicture2.FileBytes;

				picture.PictureID = _PictureService.CreateNewPicture(picture);

				if (picture.PictureID > 0)
				{
					Data.Entity.ProductPicture productPicture = new Data.Entity.ProductPicture();

					productPicture.DisplayOrder = Convert.ToInt32(TextDisplayOrder2.Text.Trim());
					productPicture.PictureID = picture.PictureID;
					productPicture.ProductID = ProductID;

					_ProductPictureService.CreateNewProductPicture(productPicture);
				}
			}

			if (fuProductPicture3.HasFile)
			{
				Data.Entity.Picture picture = new Data.Entity.Picture();

				picture.Extension = fuProductPicture3.PostedFile.ContentType;
				picture.IsNew = true;
				picture.PictureBinary = fuProductPicture3.FileBytes;

				picture.PictureID = _PictureService.CreateNewPicture(picture);

				if (picture.PictureID > 0)
				{
					Data.Entity.ProductPicture productPicture = new Data.Entity.ProductPicture();

					productPicture.DisplayOrder = Convert.ToInt32(TextDisplayOrder3.Text.Trim());
					productPicture.PictureID = picture.PictureID;
					productPicture.ProductID = ProductID;

					_ProductPictureService.CreateNewProductPicture(productPicture);
				}
			}
		}

		protected void gvwImages_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			if (_ProductPictureService == null)
			{
				_ProductPictureService = new Service.ProductPictureService();
			}

			var TextDisplayOrder = gvwImages.Rows[e.RowIndex].FindControl("TextDisplayOrder") as TextBox;
			var hfProductPictureId = gvwImages.Rows[e.RowIndex].FindControl("hfProductPictureId") as HiddenField;
			int pictureID = (int)gvwImages.DataKeys[e.RowIndex].Values["PictureID"];

			int displayOrder = 0;
			if (int.TryParse(TextDisplayOrder.Text.Trim(), out displayOrder))
			{
				var theProductPicture = new Data.Entity.ProductPicture();

				theProductPicture.DisplayOrder = displayOrder;
				theProductPicture.PictureID = pictureID;
				theProductPicture.ProductID = ProductID;
				theProductPicture.ProductPictureID = int.Parse(hfProductPictureId.Value);

				_ProductPictureService.UpdateProductPicture(theProductPicture);
			}
		}

		protected void gvwImages_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.DataItem != null)
			{
				var iProductPicture = e.Row.FindControl("iProductPicture") as Image;
				var btnDelete = e.Row.FindControl("btnDeletePicture") as LinkButton;
				var TextDisplayOrder = e.Row.FindControl("TextDisplayOrder") as TextBox;
				var ButtonUpdate = e.Row.FindControl("ButtonUpdate") as Button;

				var dataItem = e.Row.DataItem as Data.Entity.ProductPicture;

				iProductPicture.ImageUrl = "~/Administration/AdminPictureHandler.ashx?picturesize=90&pictureid=" + dataItem.PictureID.ToString();
				btnDelete.Text = GetLocalResourceString("Admin.DeleteButtonText");
				ButtonUpdate.Text = GetLocalResourceString("Admin.UpdateButtonText");

				TextDisplayOrder.Text = dataItem.DisplayOrder.ToString();
			}

			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Text = GetLocalResourceString("Admin.ProductPictures.ProductPictureHeaderText");
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.ProductPictures.ProductPictureDisplayOrderHeaderText");
			}
		}

		protected void gvwImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{
			int thePictureID = int.Parse(gvwImages.DataKeys[e.RowIndex].Values["PictureID"].ToString());

			if (_ProductPictureService == null)
			{
				_ProductPictureService = new Service.ProductPictureService();
			}

			var theProductPicture = new Data.Entity.ProductPicture();

			theProductPicture.PictureID = thePictureID;
			theProductPicture.ProductID = ProductID;

			_ProductPictureService.DeleteProductPicture(theProductPicture);
		}

		private void BindData()
		{
			if (ProductID > 0)
			{
				if (_ProductPictureService == null)
				{
					_ProductPictureService = new Service.ProductPictureService();
				}
				
				var data = _ProductPictureService.GetProductPictureByProduct(ProductID);

				gvwImages.DataSource = data;
				gvwImages.DataBind();
			}
		}
	}
}